/**
 * SOSMaxLogger class by Jaydee. 20.09.2009
 * You may distribute this class freely, provided it is not modified in any way
 * (including removing this header or changing the package path).
 * Please contact jaydee_flygroup[at]mail.ru prior to distributing modified
 * versions of this class.
 */
package djboom.utils {
	import flash.net.XMLSocket;
	import flash.events.Event;
	import flash.events.SecurityErrorEvent;
	import flash.events.IOErrorEvent;
	
	/**
	 * Allows to send messages to SOS Max.
	 *
	 * @author Jaydee
	 * @version 1.0
	 * @langversion ActionScript 3.0
	 * @playerversion Flash 9.0
	 */
	public class SOSMaxLogger {
		
		// ----------------------------------------------------------------------------
		// CONSTANTS
		// ----------------------------------------------------------------------------
		
		public static const CONFIG_MESSAGE:String = "config";
		public static const SYSTEM_MESSAGE:String = "system";
		public static const TRACE_MESSAGE:String = "trace";
		public static const FINE_MESSAGE:String = "fine";
		public static const FLASH_TRACE_MESSAGE:String = "flash trace";
		public static const WARNING_MESSAGE:String = "warning";
		public static const ERROR_MESSAGE:String = "error";
		public static const FATAL_MESSAGE:String = "fatal";
		public static const WARN_MESSAGE:String = "warn";
		public static const INFO_MESSAGE:String = "info";
		public static const DEBUG_MESSAGE:String = "debug";		public static const REMOVE_MESSAGE:String = "remove";
		
		// ----------------------------------------------------------------------------
		// PUBLIC PROPERTIES
		// ----------------------------------------------------------------------------
		
		
		
		// ----------------------------------------------------------------------------
		// PRIVATE PROPERTIES
		// ----------------------------------------------------------------------------
		
		
		
		// ----------------------------------------------------------------------------
		// PROTECTED PROPERTIES
		// ----------------------------------------------------------------------------
		
		protected static var _initialized:Boolean = false;
		protected static var _socket:XMLSocket;
		protected static var _messageQueue:Array;
		
		// ----------------------------------------------------------------------------
		// GETTERS AND SETTERS
		// ----------------------------------------------------------------------------
		
		public static function get connected():Boolean {
			return _socket.connected;
		}

		// ----------------------------------------------------------------------------
		// CONSTRUCTOR
		// ----------------------------------------------------------------------------
		
		public function SOSMaxLogger() {
			throw new Error("The SOSMaxLogger class is not intended to be instantiated.");
		}
		
		// ----------------------------------------------------------------------------
		// EVENT LISTENERS
		// ----------------------------------------------------------------------------
		
		
		
		// ----------------------------------------------------------------------------
		// PRIVATE METHODS
		// ----------------------------------------------------------------------------
		
		private static function socketConnectListener(event:Event):void {
			sendComplexMessage("SOSMaxLogger has successfully connected to server.", INFO_MESSAGE);
			
			if (_messageQueue.length > 0) {
				var numberOfQueuedMessages:int = _messageQueue.length;
				for (var i:int = 0; i < numberOfQueuedMessages; i++) {
					_socket.send(_messageQueue[i]);
				}
			}
			_messageQueue = null;
		}
		
		private static function socketSecurityErrorListener(event:Event):void {
			throw new Error("SOSMaxLogger is unable to send message to server because of occured Security error.");
		}
		
		private static function socketIOErrorListener(event:Event):void {
			throw new Error("SOSMaxLogger is unable to send message to server because of occured IO error.");
		}
		
		// ----------------------------------------------------------------------------
		// PUBLIC METHODS
		// ----------------------------------------------------------------------------
		
		public static function init(host:String = "localhost", port:int = 4444):void {
			if (!_initialized) {
				_initialized = true;
				_messageQueue = [];
				_socket = new XMLSocket();
				_socket.addEventListener(Event.CONNECT, socketConnectListener, false, 0, true);
				_socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, socketSecurityErrorListener, false, 0, true);
				_socket.addEventListener(IOErrorEvent.IO_ERROR, socketIOErrorListener, false, 0, true);
				try {
					_socket.connect(host, port);
				} catch (error:SecurityError) {
					throw new Error("SOSMaxLogger is unable to connect to " + host + " on port " + port + " because of security sandbox issues.");
				}	
			}
		}
		
		public static function sendSimpleMessage(message:String):void {
			if (_socket.connected) {
				_socket.send(message);
			} else if (_initialized) {
				_messageQueue.push(message);
			}
		}
		
		public static function sendComplexMessage(message:String, keyType:String):void {
			var formattedMessage:String = "!SOS<showMessage key='" + keyType + "'>" + message + "</showMessage>";
			if (_socket.connected) {
				_socket.send(formattedMessage);
			} else if (_initialized) {
				_messageQueue.push(formattedMessage);
			}
		}
		
		public static function sendFoldedMessage(title:String, message:String, keyType:String = FLASH_TRACE_MESSAGE) : void {
			var formattedMessage:String = "!SOS<showFoldMessage key='" + keyType + "'><title>" + title + "</title><message>" + message + "</message></showFoldMessage>";
			if (_socket.connected) {
				_socket.send(formattedMessage);
			} else if (_initialized) {
				_messageQueue.push(formattedMessage);
			}
		}
		
		// ----------------------------------------------------------------------------
		// OVERRIDE METHODS 
		// ----------------------------------------------------------------------------
		
		
	}
}
